/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2016-2026 hmc,Inc.All Rights Reserved.
 */
package com.sys.midware.rdb.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sys.midware.config.core.ConfigContainer;
import com.sys.midware.config.core.ConfigKey;
import com.sys.midware.config.model.BNode;
import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;

/**
 * 
 * <b>ClassName：</b> ConfigMongoUtil <br/>
 * <b>Description：</b> 获取配置信息工具类 <br/>
 * <b>@author：</b> mobing <br/>
 * <b>@date：</b> 2015年10月19日 下午2:55:02 <br/>
 * <b>@version: </b> <br/>
 */
public class RDBConfigUtil {
    private static final Logger logger = LoggerFactory.getLogger(RDBConfigUtil.class);

    /**
     * ConfigContainer instance
     */
    private static ConfigContainer cfg = ConfigContainer.getInstance(ConfigKey.ConfigFile.CONFIG_FILE_ROOT);

    private static final Map<String, RDBConfig> rdbConfigCached = new HashMap<String, RDBConfig>();

    private static boolean initFlag;

    public static Map<String, RDBConfig> initConfig() {
        if (initFlag) {
            return RDBConfigHolder.get();
        }

        logger.info("开始初始化RDB配置...");
        List<BNode> bnodeList = cfg.getBNodeList();
        for (BNode b : bnodeList) {
            String nodeFor = b.getNodeFor();
            if ("rdb".equalsIgnoreCase(nodeFor)) {
                RDBConfig mc = new RDBConfig();
                mc.setNodeName(b.getNodeName());
                mc.setNodeType(b.getNodeType());
                mc.setNodeDefault(b.getNodeDefault());
                // 设置链接池属性
                RDBPoolConfig poolConfig = new RDBPoolConfig();
                fillPoolProperties(poolConfig, b);
                mc.setRDBPoolConfig(poolConfig);
                rdbConfigCached.put(b.getNodeName(), mc);
            }
        }
        RDBConfigHolder.put(rdbConfigCached);
        initFlag = true;
        logger.info("RDB配置初始化完成，rdbConfigCached" + rdbConfigCached);
        return rdbConfigCached;
    }

    /**
     * 填充连接池属性配置
     * 
     * @param element
     * @param node
     * @return
     */
    private static RDBPoolConfig fillPoolProperties(RDBPoolConfig poolConfig, BNode node) {
        /*
         * 节点属性
         */
        poolConfig.setDriverClassName(node.getNodeParamStr(RDBConfigKey.KEY_DRIVER_CLASS_NAME));
        poolConfig.setUsername(node.getNodeParamStr(RDBConfigKey.KEY_USERNAME));
        poolConfig.setPassword(node.getNodeParamStr(RDBConfigKey.KEY_PASSWORD));
        poolConfig.setUrl(node.getNodeParamStr(RDBConfigKey.KEY_URL));
        /*
         * 连接池管理
         */
        poolConfig.setInitialSize(cfg.getParamInteger(RDBConfigKey.KEY_POOL_initialSize));
        poolConfig.setMaxActive(cfg.getParamInteger(RDBConfigKey.KEY_POOL_maxActive));
        poolConfig.setMaxWait(cfg.getParamInteger(RDBConfigKey.KEY_POOL_maxWait));
        poolConfig.setMinIdle(cfg.getParamInteger(RDBConfigKey.KEY_POOL_minIdle));
        poolConfig.setTimeBetweenEvictionRunsMillis(
                cfg.getParamInteger(RDBConfigKey.KEY_POOL_timeBetweenEvictionRunsMillis));
        poolConfig.setMinEvictableIdleTimeMillis(cfg.getParamInteger(RDBConfigKey.KEY_POOL_minEvictableIdleTimeMillis));
        poolConfig.setRemoveAbandonedTimeout(cfg.getParamInteger(RDBConfigKey.KEY_POOL_removeAbandonedTimeout));
        poolConfig.setRemoveAbandoned(cfg.getParamBoolean(RDBConfigKey.KEY_POOL_removeAbandoned));
        poolConfig.setLogAbandoned(cfg.getParamBoolean(RDBConfigKey.KEY_POOL_logAbandoned));
        poolConfig.setValidationQuery(cfg.getParamString(RDBConfigKey.KEY_POOL_validationQuery));
        poolConfig.setTestWhileIdle(cfg.getParamBoolean(RDBConfigKey.KEY_POOL_testWhileIdle));
        poolConfig.setTestOnBorrow(cfg.getParamBoolean(RDBConfigKey.KEY_POOL_testOnBorrow));
        poolConfig.setTestOnReturn(cfg.getParamBoolean(RDBConfigKey.KEY_POOL_testOnReturn));
        /*
         * 扩展配置
         */
        poolConfig.setPoolPreparedStatements(cfg.getParamBoolean(RDBConfigKey.KEY_POOL_poolPreparedStatements));
        poolConfig.setMaxOpenPreparedStatements(cfg.getParamInteger(RDBConfigKey.KEY_POOL_maxOpenPreparedStatements));
        poolConfig.setMaxPoolPreparedStatementPerConnectionSize(
                cfg.getParamInteger(RDBConfigKey.KEY_POOL_maxPoolPreparedStatementPerConnectionSize));
        poolConfig.setFilters(cfg.getParamString(RDBConfigKey.KEY_POOL_filters));
        return poolConfig;
    }

}
